﻿using System;
using System.Collections.Generic;
using UILib;
using UnityEngine;
using XMainClient.UI;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000FDA RID: 4058
	internal class XHUDComponent : XComponent
	{
		// Token: 0x170036CC RID: 14028
		// (get) Token: 0x0600D2BA RID: 53946 RVA: 0x00314650 File Offset: 0x00312850
		public override uint ID
		{
			get
			{
				return XHUDComponent.uuID;
			}
		}

		// Token: 0x0600D2BB RID: 53947 RVA: 0x00314667 File Offset: 0x00312867
		public static void ResetCurrentCount()
		{
			XHUDComponent.currentCount = 0;
		}

		// Token: 0x0600D2BC RID: 53948 RVA: 0x00314670 File Offset: 0x00312870
		public override void OnAttachToHost(XObject host)
		{
			base.OnAttachToHost(host);
			this._hudObject = XSingleton<XEngineCommandMgr>.singleton.GetGameObject();
			this._hudObject.layer = LayerMask.NameToLayer("UI");
			this._hudObject.name = this._entity.EngineObject.Name + "_hud";
			this.gameCamera = XSingleton<XScene>.singleton.GameCamera.UnityCamera;
			this.uiCamera = XSingleton<XGameUI>.singleton.UICamera;
			XSingleton<UiUtility>.singleton.AddChild(XSingleton<XGameUI>.singleton.UIRoot, this._hudObject.transform);
			this._BattleDoc = XDocuments.GetSpecificDocument<XBattleDocument>(XBattleDocument.uuID);
		}

		// Token: 0x0600D2BD RID: 53949 RVA: 0x00314728 File Offset: 0x00312928
		public override void OnDetachFromHost()
		{
			base.OnDetachFromHost();
			int i = this._List.Count;
			while (i > 0)
			{
				XHudEntry ent = this._List[--i];
				this.DestroyEntityHudGameObject(ent);
			}
			XSingleton<XEngineCommandMgr>.singleton.ReturnGameObject(this._hudObject);
			this._hudObject = null;
		}

		// Token: 0x0600D2BE RID: 53950 RVA: 0x00314786 File Offset: 0x00312986
		protected override void EventSubscribe()
		{
			base.RegisterEvent(XEventDefine.XEvent_HUDAdd, new XComponent.XEventHandler(this.AddHud));
			base.RegisterEvent(XEventDefine.XEvent_HUDDoodad, new XComponent.XEventHandler(this.AddDoodadHUD));
		}

		// Token: 0x0600D2BF RID: 53951 RVA: 0x003147B4 File Offset: 0x003129B4
		private XHudEntry CreateHUD()
		{
			bool flag = this._Unused.Count > 0;
			XHudEntry result;
			if (flag)
			{
				XHudEntry xhudEntry = this._Unused[this._Unused.Count - 1];
				this._Unused.RemoveAt(this._Unused.Count - 1);
				xhudEntry.time = Time.realtimeSinceStartup;
				xhudEntry.offset = 0f;
				this._List.Add(xhudEntry);
				result = xhudEntry;
			}
			else
			{
				XHudEntry xhudEntry2 = new XHudEntry();
				this._List.Add(xhudEntry2);
				result = xhudEntry2;
			}
			return result;
		}

		// Token: 0x0600D2C0 RID: 53952 RVA: 0x00314848 File Offset: 0x00312A48
		public bool AddDoodadHUD(XEventArgs e)
		{
			XHUDDoodadArgs xhuddoodadArgs = e as XHUDDoodadArgs;
			GameObject gameObject = XSingleton<XResourceLoaderMgr>.singleton.CreateFromPrefab("UI/Billboard/HUDDoodad", Vector3.zero, Quaternion.identity, true, false);
			gameObject.layer = LayerMask.NameToLayer("UI");
			XSingleton<UiUtility>.singleton.AddChild(this._hudObject, gameObject);
			XHudEntry xhudEntry = this.CreateHUD();
			xhudEntry.time = Time.realtimeSinceStartup;
			xhudEntry.stay = 0.1f;
			xhudEntry.offset = 0f;
			xhudEntry.val = 0f;
			xhudEntry.label = (gameObject.transform.GetComponent("XUILabel") as IXUILabel);
			xhudEntry.label.SetRootAsUIPanel(true);
			xhudEntry.isDigital = true;
			IXUILabelSymbol ixuilabelSymbol = gameObject.transform.GetComponent("XUILabelSymbol") as IXUILabelSymbol;
			ItemList.RowData itemConf = XBagDocument.GetItemConf(xhuddoodadArgs.itemid);
			bool flag = itemConf != null;
			if (flag)
			{
				ItemType itemType = (ItemType)itemConf.ItemType;
				if (itemType != ItemType.VIRTUAL_ITEM)
				{
					ItemList.RowData itemConf2 = XBagDocument.GetItemConf(xhuddoodadArgs.itemid);
					string itemQualityColorStr = XSingleton<UiUtility>.singleton.GetItemQualityColorStr((int)itemConf2.ItemQuality);
					ixuilabelSymbol.InputText = string.Format("[{0}]{1}[-] x{2}", itemQualityColorStr, XSingleton<UiUtility>.singleton.ChooseProfString(itemConf2.ItemName, 0U), xhuddoodadArgs.count);
				}
				else
				{
					string inputText = XLabelSymbolHelper.FormatCostWithIcon("+{1} {0}", xhuddoodadArgs.count, (ItemEnum)xhuddoodadArgs.itemid);
					ixuilabelSymbol.InputText = inputText;
				}
			}
			this.UIFollowTarget();
			IXHUDDescription ixhuddescription = gameObject.transform.GetComponent("HUDDescription") as IXHUDDescription;
			xhudEntry.offsetCurve = ixhuddescription.GetPosCurve();
			xhudEntry.scaleCurve = ixhuddescription.GetScaleCurve();
			xhudEntry.alphaCurve = ixhuddescription.GetAlphaCurve();
			return true;
		}

		// Token: 0x0600D2C1 RID: 53953 RVA: 0x00314A04 File Offset: 0x00312C04
		public bool AddHud(XEventArgs e)
		{
			bool flag = !XHUDComponent.processHud || XHUDComponent.currentCount > XHUDComponent._Max_UI_UpdateCount_PreFrame;
			bool result;
			if (flag)
			{
				e.Recycle();
				result = true;
			}
			else
			{
				bool flag2 = !XHUDComponent.processHud;
				if (flag2)
				{
					result = true;
				}
				else
				{
					bool flag3 = XHUDComponent.currentCount >= XHUDComponent._Max_UI_UpdateCount_PreFrame;
					if (flag3)
					{
						bool flag4 = this._List.Count > 0;
						if (flag4)
						{
							this.DeleteHUD(this._List[0]);
						}
					}
					XHUDAddEventArgs xhudaddEventArgs = e as XHUDAddEventArgs;
					bool flag5 = !this._entity.IsPlayer && (!XSingleton<XScene>.singleton.bSpectator || XSingleton<XEntityMgr>.singleton.Player.WatchTo == null || XSingleton<XEntityMgr>.singleton.Player.WatchTo.ID != this._entity.ID);
					if (flag5)
					{
						ulong num = 0UL;
						bool flag6 = false;
						bool flag7 = xhudaddEventArgs.caster == null || xhudaddEventArgs.caster.Attributes == null || xhudaddEventArgs.caster.Attributes.HostID == 0UL;
						if (flag7)
						{
							bool flag8 = !this._BattleDoc.ShowTeamMemberDamageHUD;
							if (flag8)
							{
								num = xhudaddEventArgs.damageResult.Caster;
							}
							else
							{
								flag6 = true;
							}
						}
						else
						{
							bool showMobDamageHUD = this._BattleDoc.ShowMobDamageHUD;
							if (!showMobDamageHUD)
							{
								return true;
							}
							num = xhudaddEventArgs.caster.Attributes.HostID;
						}
						bool flag9 = !flag6;
						if (flag9)
						{
							bool flag10 = num > 0UL;
							if (!flag10)
							{
								return true;
							}
							bool flag11 = XSingleton<XEntityMgr>.singleton.Player.WatchTo != null && XSingleton<XEntityMgr>.singleton.Player.WatchTo.ID == num;
							bool flag12 = num != XSingleton<XAttributeMgr>.singleton.XPlayerData.RoleID && !flag11;
							if (flag12)
							{
								return true;
							}
						}
					}
					GameObject hudtemplateByDamageResult = XSingleton<XCombatHUDMgr>.singleton.GetHUDTemplateByDamageResult(xhudaddEventArgs.damageResult, this._entity.IsPlayer);
					XHUDComponent.currentCount++;
					hudtemplateByDamageResult.layer = LayerMask.NameToLayer("UI");
					XHudEntry xhudEntry = this.CreateHUD();
					xhudEntry.time = Time.realtimeSinceStartup;
					xhudEntry.stay = 0.1f;
					xhudEntry.offset = 0f;
					xhudEntry.val = (float)xhudaddEventArgs.damageResult.Value;
					bool flag13 = !xhudEntry.init;
					if (flag13)
					{
						xhudEntry.label = (hudtemplateByDamageResult.transform.GetComponent("XUILabel") as IXUILabel);
						xhudEntry.label.SetRootAsUIPanel(true);
					}
					XSingleton<UiUtility>.singleton.AddChildNoMark(this._hudObject, hudtemplateByDamageResult);
					xhudEntry.isDigital = true;
					string text = XSingleton<XCombatHUDMgr>.singleton.GetHUDText(xhudaddEventArgs.damageResult, xhudEntry.isDigital);
					bool isMainViewEntity = this._entity.IsMainViewEntity;
					if (isMainViewEntity)
					{
						text = ((xhudaddEventArgs.damageResult.Value >= 0.0) ? "-" : "+") + text;
					}
					bool flag14 = xhudaddEventArgs.damageResult.Result != ProjectResultType.PJRES_IMMORTAL && xhudaddEventArgs.damageResult.Result != ProjectResultType.PJRES_MISS;
					if (flag14)
					{
						xhudEntry.label.SetText(text);
					}
					bool flag15 = this._entity.IsEnemy && xhudaddEventArgs.damageResult.Value > 0.0;
					if (flag15)
					{
						XSingleton<XLevelStatistics>.singleton.ls._total_damage += (float)xhudaddEventArgs.damageResult.Value;
					}
					else
					{
						bool isPlayer = this._entity.IsPlayer;
						if (isPlayer)
						{
							bool flag16 = xhudaddEventArgs.damageResult.Value > 0.0;
							if (flag16)
							{
								XSingleton<XLevelStatistics>.singleton.ls._total_hurt += (float)xhudaddEventArgs.damageResult.Value;
							}
							else
							{
								XSingleton<XLevelStatistics>.singleton.ls._total_heal += (float)(-(float)xhudaddEventArgs.damageResult.Value);
							}
						}
					}
					bool flag17 = !xhudaddEventArgs.damageResult.IsCritical() && !this._entity.IsMainViewEntity;
					if (flag17)
					{
						bool bEnable = false;
						Color white = Color.white;
						Color white2 = Color.white;
						XSingleton<XCombatHUDMgr>.singleton.GetElementColor(xhudaddEventArgs.damageResult.ElementType, ref bEnable, ref white, ref white2);
						xhudEntry.label.SetGradient(bEnable, white, white2);
						xhudEntry.label.SetColor(white);
					}
					bool flag18 = !xhudaddEventArgs.damageResult.IsCritical() && xhudaddEventArgs.damageResult.Result != ProjectResultType.PJRES_IMMORTAL && this._entity.IsMainViewEntity;
					if (flag18)
					{
						xhudEntry.label.SetGradient(false, Color.white, Color.white);
						xhudEntry.label.SetColor(new Color32(byte.MaxValue, 32, 73, byte.MaxValue));
					}
					bool flag19 = xhudaddEventArgs.damageResult.Value < 0.0;
					if (flag19)
					{
						Color green = Color.green;
						Color green2 = Color.green;
						xhudEntry.label.SetGradient(true, green, green2);
						xhudEntry.label.SetColor(green);
					}
					this.UIFollowTarget();
					bool flag20 = !xhudEntry.init;
					if (flag20)
					{
						IXHUDDescription ixhuddescription = hudtemplateByDamageResult.transform.GetComponent("HUDDescription") as IXHUDDescription;
						xhudEntry.offsetCurve = ixhuddescription.GetPosCurve();
						xhudEntry.alphaCurve = ixhuddescription.GetAlphaCurve();
						xhudEntry.scaleCurve = ixhuddescription.GetScaleCurve();
					}
					result = true;
				}
			}
			return result;
		}

		// Token: 0x0600D2C2 RID: 53954 RVA: 0x00314FC1 File Offset: 0x003131C1
		private void DeleteHUD(XHudEntry ent)
		{
			this._List.Remove(ent);
			this._Unused.Add(ent);
			this.DestroyEntityHudGameObject(ent);
			XHUDComponent.currentCount--;
		}

		// Token: 0x0600D2C3 RID: 53955 RVA: 0x00314FF2 File Offset: 0x003131F2
		protected void HideEntityHudGameObject(XHudEntry ent)
		{
			ent.label.gameObject.transform.localPosition = XResourceLoaderMgr.Far_Far_Away;
			XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroy(ent.label.gameObject, true, false);
		}

		// Token: 0x0600D2C4 RID: 53956 RVA: 0x00314FF2 File Offset: 0x003131F2
		protected void DestroyEntityHudGameObject(XHudEntry ent)
		{
			ent.label.gameObject.transform.localPosition = XResourceLoaderMgr.Far_Far_Away;
			XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroy(ent.label.gameObject, true, false);
		}

		// Token: 0x0600D2C5 RID: 53957 RVA: 0x00315028 File Offset: 0x00313228
		private void UIFollowTarget()
		{
			bool flag = this.gameCamera == null;
			if (flag)
			{
				this.gameCamera = XSingleton<XScene>.singleton.GameCamera.UnityCamera;
			}
			float height = this._entity.Height;
			Vector3 radiusCenter = this._entity.RadiusCenter;
			Vector3 vector = new Vector3(radiusCenter.x, radiusCenter.y + height, radiusCenter.z);
			Vector3 vector2 = this.gameCamera.WorldToViewportPoint(vector);
			if (vector2.z > 0f)
			{
				bool flag2 = vector2.x > 0f && vector2.x < 1f && vector2.y > 0f && vector2.y < 1f;
			}
			this._hudObject.transform.position = this.uiCamera.ViewportToWorldPoint(vector2);
			vector2 = this._hudObject.transform.localPosition;
			vector2.x = (float)Mathf.FloorToInt(vector2.x);
			vector2.y = (float)Mathf.FloorToInt(vector2.y);
			bool flag3 = vector2.y > (float)(XSingleton<XGameUI>.singleton.Base_UI_Height / 2) * 0.5f;
			if (flag3)
			{
				vector2.y = (float)(XSingleton<XGameUI>.singleton.Base_UI_Height / 2) * 0.5f;
			}
			vector2.z = 0f;
			this._hudObject.transform.localPosition = vector2;
		}

		// Token: 0x0600D2C6 RID: 53958 RVA: 0x00315198 File Offset: 0x00313398
		private void UpdateHUDs()
		{
			float realtimeSinceStartup = Time.realtimeSinceStartup;
			int i = this._List.Count;
			while (i > 0)
			{
				XHudEntry xhudEntry = this._List[--i];
				float time = xhudEntry.offsetCurve[xhudEntry.offsetCurve.length - 1].time;
				float time2 = xhudEntry.scaleCurve[xhudEntry.scaleCurve.length - 1].time;
				float time3 = xhudEntry.alphaCurve[xhudEntry.alphaCurve.length - 1].time;
				float num = Mathf.Max(Mathf.Max(time2, time), time3);
				float num2 = realtimeSinceStartup - xhudEntry.movementStart;
				xhudEntry.offset = xhudEntry.offsetCurve.Evaluate(num2);
				float num3 = xhudEntry.scaleCurve.Evaluate(realtimeSinceStartup - xhudEntry.time);
				bool flag = num3 < 0.001f;
				if (flag)
				{
					num3 = 0.001f;
				}
				xhudEntry.label.gameObject.transform.localScale = Vector3.one * num3;
				float num4 = xhudEntry.alphaCurve.Evaluate(realtimeSinceStartup - xhudEntry.time);
				Color color = xhudEntry.label.GetColor();
				Color color2 = new Color(color.r, color.g, color.b, num4);
				xhudEntry.label.SetColor(color2);
				bool flag2 = num2 > num;
				if (flag2)
				{
					this.DeleteHUD(xhudEntry);
				}
			}
			float num5 = -200f;
			int j = this._List.Count;
			while (j > 0)
			{
				XHudEntry xhudEntry2 = this._List[--j];
				num5 = Mathf.Max(num5, xhudEntry2.offset);
				xhudEntry2.label.gameObject.transform.localPosition = new Vector3(0f, num5, 0f);
				num5 += Mathf.Round(xhudEntry2.label.gameObject.transform.localScale.y * 20f);
			}
		}

		// Token: 0x0600D2C7 RID: 53959 RVA: 0x003153BA File Offset: 0x003135BA
		public override void PostUpdate(float fDeltaT)
		{
			this.UpdateHUDs();
		}

		// Token: 0x04005FBE RID: 24510
		public new static readonly uint uuID = XSingleton<XCommon>.singleton.XHash("CombatInfoInScene");

		// Token: 0x04005FBF RID: 24511
		private GameObject _hudObject = null;

		// Token: 0x04005FC0 RID: 24512
		private List<XHudEntry> _List = new List<XHudEntry>();

		// Token: 0x04005FC1 RID: 24513
		private List<XHudEntry> _Unused = new List<XHudEntry>();

		// Token: 0x04005FC2 RID: 24514
		private Camera gameCamera = null;

		// Token: 0x04005FC3 RID: 24515
		private Camera uiCamera = null;

		// Token: 0x04005FC4 RID: 24516
		private XBattleDocument _BattleDoc;

		// Token: 0x04005FC5 RID: 24517
		public static bool processHud = true;

		// Token: 0x04005FC6 RID: 24518
		private static int currentCount = 0;

		// Token: 0x04005FC7 RID: 24519
		public static int _Max_UI_UpdateCount_PreFrame = 4;
	}
}
